import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:health_flutter_lib/native/nativechannel.dart';

class S {
  static Map<String, String> langdata = {};
  static String language = "en";
  static String calendarLanguage = "en_US";

  /*
  * 根据输入语言加载对应的assets里面的语言包
  * */
  static void loadLang(BuildContext context, String lang, Function callback) {
    String key = Native.HealthDataApp == 1 ? "assets/lang/$lang.json" : "health_flutter_lib/assets/lang/$lang.json";
    DefaultAssetBundle.of(context).loadString(key).then((value) {
      var data = json.decode(value);
      data.forEach((key, value) {
        langdata[key] = value.toString();
      });
      callback();
    });
  }

  /*
  * 获取字符串
  * */
  static String of(String id, String defaultvalue) {
    return langdata[id] ?? defaultvalue;
  }

  // 中文简体："zh"（通常配合国家代码 CN 使用，如 Locale('zh', 'CN')）
  // 中文繁体："zh"（通常配合国家代码 TW 或 HK 使用，如 Locale('zh', 'TW')）
  // 英语："en"
  // 日语："ja"
  // 德语："de"
  // 意大利语："it"
  // 阿拉伯语："ar"
  // 芬兰语："fi"
  // 波兰语："pl"
  // 俄语："ru"
  // 越南语："vi"
  // 泰语："th"
  // 捷克语："cs"
  // 法语："fr"
  // 西班牙语："es"
  // 葡萄牙语："pt"
  // 韩语："ko"
  // 土耳其语："tr"
  static String getLangFileName(Locale locale) {
    if (locale.languageCode.contains('zh')) {
      if(locale.countryCode == 'CN'){
        language = "zh_CN";
      }else{
        language = "zh_TW";
      }
    }else if(locale.languageCode.contains('ja')){//日语
      language = "ja";
    }else if(locale.languageCode.contains('de')){//德语
      language = "de";
    }else if(locale.languageCode.contains('it')){//意大利语
      language = "it";
    }else if(locale.languageCode.contains('ar')){//阿拉伯语
      language = "ar";
    }else if(locale.languageCode.contains('fi')){//芬兰语
      language = "fi";
    }else if(locale.languageCode.contains('pl')){//波兰语
      language = "pl";
    }else if(locale.languageCode.contains('ru')){//俄语
      language = "ru";
    }else if(locale.languageCode.contains('ru')){//越南语
      language = "vi";
    }else if(locale.languageCode.contains('th')){//泰语
      language = "th";
    }else if(locale.languageCode.contains('cs')){//捷克语
      language = "cs";
    }else if(locale.languageCode.contains('fr')){//法语
      language = "fr";
    }else if(locale.languageCode.contains('es')){//西班牙语
      language = "es";
    }else if(locale.languageCode.contains('pt')){//葡萄牙语
      language = "pt";
    }else if(locale.languageCode.contains('ko')){//韩语
      language = "ko";
    }else if(locale.languageCode.contains('tr')){//土耳其语
      language = "tr";
    } else {
      language = "en";
    }
    calendarLanguage = "${locale.languageCode}_${locale.countryCode}";
    return language;
  }

  static String getServerLanguage() {
    if ("en" == language) {
      return 'en_WW';
    } else {
      return 'zh_CN';
    }
  }

  static String getCalendarLocale(){
    return calendarLanguage;
  }

  static String getAppLanguage() {
    if (language.contains('zh')) {
      return "zh";
    }
    return "en";
  }

  static bool isCN() {
    if (language.contains('zh')) {
      return true;
    }
    return false;
  }
}
